<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;

class GoodsController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 40000  
         * 40001  
         * 40002  
         */
    }

    /*
     * 小程序售卖商品列表
     * author xfwed
     * date 20120.03.09
     */

    public function goodsList() {
        $rule = [
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $where['is_xcxsell'] = 1;
        $where['verifyStatus'] = 2;
        $where['valid'] = 1;
        $where['is_smallshopsell'] = 2;
        $where['is_dis'] = 2;
        
        $perPage = isset($all['perPage']) ? $all['perPage'] : 10;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        $list = Db::table('goods_info')
                ->where($where)
                ->field('id,name,summary,icon,sell_count')
                ->limit($offset, $perPage)
                ->select();
        foreach ($list as $k => $v){
            $row = Db::table('goods_info_detail')
                    ->where('goodsId',$v['id'])
                    ->field('oriPrice,price')
                    ->order('price asc')
                    ->find();
            $list[$k]['oriPrice'] = $row['oriPrice'];
            $list[$k]['price'] = $row['price'];
        }
        
        return Utils::ajaxReturn(0, '查询成功', $list);
    }

}
